package output;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.text.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

import commandExecution.CommandGenerator;
 
public class CreatePDF {
	
	//String date;
	Date d;
	Document document;
	String[] command = {"/bin/sh", "-c","mkdir /Users/giuliabiffi/Desktop/rapporto_pdf"};
	String[] command_remove = {"/bin/sh", "-c","rm /Users/giuliabiffi/Desktop/risultati_ricerca.txt"};
	Paragraph par; 
	
	public CreatePDF(){
		
		
		d = new Date();
		document = new Document(PageSize.A4, 50, 50, 50, 50);
		try {
			Process p = Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
  
	public void create(String name){
		
		try {
			  
			  
			  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/Users/giuliabiffi/Desktop/rapporto_pdf/risultati_ricerca.pdf"));
              
			  
	         // PdfWriter.getInstance(document, new FileOutputStream("/Users/giuliabiffi/Desktop/my_folder/risultati_ricerca.pdf"));
	          Paragraph header = new Paragraph("Ricerca eseguita da " +name+ " in data " + SimpleDateFormat.getDateInstance(DateFormat.LONG, Locale.ITALIAN).format(d)+" alle ore "+DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.ITALIAN).format(d)+"sec");
	          header.setAlignment(Element.ALIGN_CENTER);
	          Paragraph title = new Paragraph("Risultati della ricerca");
	          title.setAlignment(Element.ALIGN_CENTER);
	          document.open();
	          document.add(header);
	          document.add(title);
	          
	          PdfContentByte cb = writer.getDirectContent(); 
	          BufferedReader reader =new BufferedReader(new FileReader("/Users/giuliabiffi/Desktop/risultati_ricerca.txt")); 
	          String line; 
	  
	          float pos;
	          while ((line = reader.readLine()) != null) { 
	          	par = new Paragraph(line+"\n"); 
	          	par.setAlignment(Element.ALIGN_JUSTIFIED); 
	          	document.add(par);
	          	document.add(new Paragraph());
	          	pos = writer.getVerticalPosition(false); 
	          	System.out.println(pos); 
	          	//cb.moveTo(0, pos); 
	          //	cb.lineTo(PageSize.A4.getWidth(), pos); 
	          		if (pos < 90) document.newPage();
	          }
	          
	          
	          document.close();
	          //Process r = Runtime.getRuntime().exec(command_remove);
	       } catch (Exception e) {
	          System.out.println(e);
	       }
	}
	
	
       
   
}

